package com.hx.voice.recognition.auth.common.util.jwt;

import com.hx.voice.recognition.auth.common.constants.CommonConstants;
import com.hx.voice.recognition.auth.common.util.StringHelper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;

/**
 * @descripe：JWT生成器 私钥加密 公钥解密
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/10 15:25
 * @version：V1.0
 */
public class JWTHelper  {

    private static RsaKeyHelper rsaKeyHelper = new RsaKeyHelper();

    /**
     * @description 生成token
     * @method  generateToken
     * @param  [ijwtInfo, privateKeyPath, expire]
     * @return java.lang.String
     * @date: 2019/6/10 15:28
     * @author:zhoujinbing
     */
    public static String generateToken(IJWTInfo ijwtInfo,String privateKeyPath,int expire) throws Exception {
        String compactJws = Jwts.builder()
                .setSubject(ijwtInfo.getUniqueName())
                .claim(CommonConstants.JWT_KEY_USER_ID,ijwtInfo.getId())
                .claim(CommonConstants.JWT_KEY_NAME,ijwtInfo.getName())
                .setExpiration(DateTime.now().plusSeconds(expire).toDate())
                .signWith(SignatureAlgorithm.RS256,rsaKeyHelper.getPrivateKey(privateKeyPath))
                .compact();
        return compactJws;
    }

    /**
     * @description 生成token
     * @method  generateToken
     * @param  [ijwtInfo, privateKeyBytes, expire]
     * @return java.lang.String
     * @date: 2019/6/10 15:28
     * @author:zhoujinbing
     */
    public static String generateToken(IJWTInfo ijwtInfo,byte[] privateKeyByte,int expire) throws Exception {
        String compactJws = Jwts.builder()
                .setSubject(ijwtInfo.getUniqueName())
                .claim(CommonConstants.JWT_KEY_USER_ID,ijwtInfo.getId())
                .claim(CommonConstants.JWT_KEY_NAME,ijwtInfo.getName())
                .setExpiration(DateTime.now().plusSeconds(expire).toDate())
                .signWith(SignatureAlgorithm.RS256,rsaKeyHelper.getPrivateKey(privateKeyByte))
                .compact();
        return compactJws;
    }

    /**
     * @description 公钥解析token
     * @method  parserToken
     * @param  [token, pubKeyPath]
     * @return io.jsonwebtoken.Jws<io.jsonwebtoken.Claims>
     * @date: 2019/6/10 15:47
     * @author:zhoujinbing
     */
    public static Jws<Claims> parserToken(String token,String pubKeyPath) throws Exception {
        Jws<Claims> parseClaimsJws = Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKeyPath)).parseClaimsJws(token);
        return parseClaimsJws;
    }

    /**
     * @description 公钥解析token
     * @method  parserToken
     * @param  [token, pubKeyByte]
     * @return io.jsonwebtoken.Jws<io.jsonwebtoken.Claims>
     * @date: 2019/6/10 15:47
     * @author:zhoujinbing
     */
    public static Jws<Claims> parserToken(String token,byte[] pubKeyByte) throws Exception {
        Jws<Claims> parseClaimsJws = Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKeyByte)).parseClaimsJws(token);
        return parseClaimsJws;
    }

    /**
     * @description 从token中获取用户信息
     * @method  getInfoFormToken
     * @param  [token, pubKeyPath]
     * @return IJWTInfo
     * @date: 2019/6/10 15:51
     * @author:zhoujinbing
     */
    public static IJWTInfo getInfoFormToken(String token,String pubKeyPath) throws Exception {

        Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
        Claims body = claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StringHelper.getObjectValue(CommonConstants.JWT_KEY_USER_ID),StringHelper.getObjectValue(CommonConstants.JWT_KEY_NAME));
    }

    /**
     * @description 从token中获取用户信息
     * @method  getInfoFormToken
     * @param  [token, pubKeyByte]
     * @return IJWTInfo
     * @date: 2019/6/10 15:51
     * @author:zhoujinbing
     */
    public static IJWTInfo getInfoFormToken(String token,byte[] pubKeyByte) throws Exception {

        Jws<Claims> claimsJws = parserToken(token, pubKeyByte);
        Claims body = claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StringHelper.getObjectValue(CommonConstants.JWT_KEY_USER_ID),StringHelper.getObjectValue(CommonConstants.JWT_KEY_NAME));
    }






}